﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FlyHorse.LIMS.Model;
using FlyHorse.LIMS.WinApp.LanguageResource.LoginFormRes;

namespace FlyHorse.LIMS.WinApp.OtherForms
{
    public partial class LoginForm : DevExpress.XtraEditors.XtraForm
    {
        BLL.LoginUserBll loginUserBll = new BLL.LoginUserBll();
        public LoginForm()
        {
            //System.Globalization.CultureInfo s = System.Threading.Thread.CurrentThread.CurrentUICulture;
            InitializeComponent();
        }

        private void simpleButton_login_Click(object sender, EventArgs e)
        {
            string userName = this.textEdit_UserName.Text.Trim();
            string password = this.textEdit_PassWord.Text;

            LoginUser loginUser= loginUserBll.LoadEntities(m => m.UserName == userName ).FirstOrDefault();
            if (loginUser==null)
            {
                XtraMessageBox.Show(ResourceLoginForm.UserNameErr);
                return;
            }
            if (loginUser.Password==password)
            {
                if (this.comboBoxEdit_language.Text == "English")
                {
                    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
                }
                else
                {
                    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-Hans");   //zh-CN
                }
                FlyHorse.LIMS.WinApp.Utils.LoginUserUtil.LoginUser = loginUser;
                Form1 form = new Form1();
                form.Show();
                using (OJCB4Entities db =new OJCB4Entities())
                {
                   var uers=  db.LoginUsers.Find(loginUser.LoginUserID);
                   uers.LastLoginTime = DateTime.Now;
                   db.SaveChanges();
                }
                this.Hide();
            }
            else
            {
                XtraMessageBox.Show(ResourceLoginForm.ErrPassWord);
                this.textEdit_PassWord.Focus();
                return;
            }
        }

        private void textEdit_PassWord_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)//如果输入的是回车键  
            {
                this.simpleButton_login_Click(sender, e);//触发button事件  
            }  
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            //基础提供程序在 Open 上失败。
            this.textEdit_UserName.Properties.DataSource = loginUserBll.LoadEntities(m => m.IsDelete == false).ToList();
        }

        private void comboBoxEdit1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (this.comboBoxEdit_language.Text=="English")
            {
                this.Text = "LoginForm";
                this.layoutControlItem4.Text = "Language";
                this.layoutControlItem1.Text = "User Name";
                layoutControlItem2.Text = "Password";
                simpleButton_login.Text = "Login";
            }
            else 
            {
                this.Text = "用户登录";
                this.layoutControlItem4.Text = "语言";
                this.layoutControlItem1.Text = "用户名";
                layoutControlItem2.Text = "密码";
                simpleButton_login.Text = "登录";
            }
        }
    }
}